//
//  CommentTableViewCell.m
//  Cycling
//
//  Created by 迟国强 on 2023/4/5.
//

#import "CommentTableViewCell.h"
#import "CommentItemCell.h"
#import "RouteDetailModel.h"
#import "RouteRequest.h"
#import "CommentView.h"
#import "MoreCommentCollectionCell.h"

@implementation CommentTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.firstCommentNum = 0;
    self.bgView.layer.cornerRadius = 16;
    self.bgView.layer.masksToBounds = YES;
    [self.collectionView registerNib:[UINib nibWithNibName:@"CommentItemCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CommentItemCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"MoreCommentCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"MoreCommentCollectionCell"];
    
    self.noDataL.text = CycLocalizedString(@"No comments", @"暂无评论");

    // Initialization code
}
- (IBAction)commentBtnClicked:(UIButton *)sender {
    self.block(self.detailModel);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setModels:(NSArray *)models {
    _models = models;
    for (int i = 0; i < models.count; i++) {
        RouteCommentModel *model = models[i];
        if (model.isSuper) {
            self.firstCommentNum ++;
        }
    }
    [self.collectionView reloadData];
}

- (void)setDetailModel:(RouteDetailModel *)detailModel {
    _detailModel = detailModel;
    self.commentL.text = [detailModel.commnum intValue] == 0 ? CycLocalizedString(@"Comments", @"评论") : [NSString stringWithFormat:CycLocalizedString(@"Comments(%d)", @"评论（%d)"), [detailModel.commnum intValue]];

    self.noDataL.hidden = [detailModel.commnum intValue] != 0;
    
    [self.collectionView reloadData];
}

#pragma mark

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.row == self.models.count) {
        MoreCommentCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MoreCommentCollectionCell" forIndexPath:indexPath];
        cell.titleLB.text = CycLocalizedString(@"More comments", @"更多评论");
        return cell;
    } else {
        CommentItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CommentItemCell" forIndexPath:indexPath];
        cell.detailModel = self.detailModel;
        cell.model = self.models[indexPath.row];
        cell.block = ^(RouteCommentModel * _Nonnull model) {
            [CommentView CommentWith:NSObject.getKeyWindowRootViewController Complection:^(NSString * _Nonnull content) {
                [self commentRouteWith:content commentId:model.id];
                [NSObject.getKeyWindowRootViewController gq_removeBgView];
            }];
        };
        cell.numBlock = ^(RouteCommentModel * _Nonnull model) {
            if (self.firstCommentNum < 3) {
                [CommentView CommentWith:NSObject.getKeyWindowRootViewController Complection:^(NSString * _Nonnull content) {
                    [self commentRouteWith:content commentId:model.id];
                    [NSObject.getKeyWindowRootViewController gq_removeBgView];
                }];
            } else {
                self.listBlock(model);
            }
        };
        cell.commentBtn.hidden = YES;
        return cell;
    }
    
}
- (void)commentRouteWith:(NSString *)text commentId:(NSString *)recid {
    [RouteRequest commentsRouteWithRouteId:self.detailModel.id text:text recid:recid result:^(BOOL success) {
        if (success) {
            [self showHudTipStr:@"评论成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"CommentSubNotification" object:nil];
        }
    }];
}
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.firstCommentNum < 3 ? self.models.count : self.models.count + 1;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.models.count) {
        self.moreBlock(self.detailModel);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == self.models.count) {
        return CGSizeMake(SCREEN_W - 36, 60);
    } else {
        RouteCommentModel *model = self.models[indexPath.row];
        CGSize size = [model.content boundingRectWithSize:CGSizeMake(SCREEN_W-56, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:PingFangFont(20)} context:nil].size;
        return CGSizeMake(SCREEN_W - 36, 90 + size.height);
    }
}

@end
